﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <#include "../common/header.ftl">
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#assign menu="employee">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">员工编辑</h1>
                </div>
            </div>
            <div class="row col-sm-10">
                <form class="form-horizontal" action="/employee/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" value="${(entity.id)!}" name="id">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">用户名：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" value="${(entity.name)!}"
                                   placeholder="请输入用户名">
                        </div>
                    </div>
                <#--只有在新增员工时才显示输入密码-->
                    <#if !entity??>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">密码：</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control" id="pwd" name="password" placeholder="请输入密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">验证密码：</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control" name="repwd" placeholder="再输入一遍密码">
                        </div>
                    </div>
                    </#if>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">Email：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="email" value="${(entity.email)!}"
                                   placeholder="请输入邮箱">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">年龄：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="age" value="${(entity.age)!}"
                                   placeholder="请输入年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">部门：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="dept.id">
                                    <#list depts as d>
                                    <option value="${d.id}">${d.name}</option>
                                    </#list>
                            </select>
                        </div>
                        <script>
                            //部门回显
                            $('#editForm select[name="dept.id"]').val(${(entity.dept.id)!});
                        </script>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">超级管理员：</label>
                        <label class="checkbox-inline" style="margin-left: 15px;">
                            <input type="checkbox" id="admin" name="admin">
                        </label>
                    </div>
                    <div class="form-group" id="role">
                        <div>
                            <label class="control-label" style="margin-left: 60px">角色：</label>
                        </div>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-4 col-sm-offset-1">
                            <#--显示系统中所有的角色-->
                                <select multiple class="form-control allRoles" size="15">
                                    <#list roles as entity>
                                        <option value="${entity.id}">${entity.name}</option>
                                    </#list>
                                </select>
                            </div>
                            <div class="col-sm-2" style="margin-top: 60px;" align="center">
                                <div>
                                    <a type="button" class="btn btn-info  " style="margin-top: 10px"
                                       onclick="moveSelected('allRoles', 'selfRoles')">&nbsp;&gt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveSelected('selfRoles', 'allRoles')">&nbsp;&lt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('allRoles', 'selfRoles')">&gt;&gt;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('selfRoles', 'allRoles')">&lt;&lt;</a>
                                </div>
                            </div>

                            <div class="col-sm-4">
                                <select multiple class="form-control selfRoles" size="15" name="ids">
                                <#--显示当前用户拥有的角色-->
                                        <#list (entity.roles)! as role>
                                    <option value="${(role.id)!}">${(role.name)!}</option>
                                        </#list>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button id="btn_submit" type="submit" class="btn btn-success">
                                <span class="glyphicon glyphicon-saved"></span> 保存
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>

    function moveAll(src, target) {
        $('.' + src + " option").appendTo($('.' + target));
    }

    function moveSelected(src, target) {
        $('.' + src + " option:selected").appendTo($('.' + target));
    }

    $(function () {
        //表单校验

        $('#btn_submit').click(function () {
            // 默认把右侧选中再提交
            $('.selfRoles option').prop('selected', true);
            /*$('#editForm').submit();*/
        });

        $('#editForm').validate(
                {
                    rules: {
                        name:{
                            required:true,
                            minlength:2
                        },
                        password:{
                            required:true,
                            minlength:1
                        },
                        repwd:{
                            required:true,
                            equalTo:'#pwd'
                        },
                        email:{
                            required:true,
                            email:true
                        },
                        age:{
                            required:true,
                            min:0,
                            max:100
                        }


                    },
                    messages:{
                        name:{
                            required:'用户名必填',
                            minlength:'亲,最小长度是4'
                        },password:{
                            required:'密码必填',
                            minlength:'最小长度6'
                        },
                        repwd:{
                            required:'重复密码必填',
                            equalTo:'两次密码不一致'
                        },
                        email:{
                            required:'必填项目',
                            email:'格式不对'
                        },
                        age:{
                            required:'年龄必填',
                            min:'最小值0岁',
                            max:'最大值有问题'
                        }
                    },
                    submitHandler:function (form) {
                        $(form).ajaxSubmit(function (data) {
                            handleResult2(data,'/employee/list.do');
                        })
                    }
                })

    });
    /*设置超级管理员和角色的显示*/
    var role = $('#role');
    $('#admin').click(function () {
        if (this.checked) {
            //去掉角色
            $('#role').remove();
        } else {
            //还原角色
            $(this).closest('div').after(role);
        }
    });

    // 角色去重
    $('.selfRoles option').each(function (i, ele) {
        $('.allRoles option[value="' + ele.value + '"]').remove();
        //console.log(ele.value);

    });

    //显示超管
            <#if entity?? && entity.admin>
            $('#admin').click();
            </#if>
</script>
</body>
</html>